package ch.il07.converter.persistance.tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import ch.il07.converter.persistence.Persistence;
import ch.il07.converter.persistence.PersistenceHelper;
import ch.il07.converter.persistence.entities.Measurand;
import ch.il07.converter.persistence.entities.Unit;
import ch.il07.converter.persistence.exceptions.AlreadyDefinedException;
import ch.il07.converter.persistence.exceptions.PersistenceException;
import ch.il07.utils.RandomHelper;

public class GetUnits {

	private Persistence persistence;

	@Before
	public void setUp() {
		try {
			persistence = PersistenceHelper.getPersistence();
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void normalGetter() {
		Measurand m = new Measurand(RandomHelper.randString(), new Unit(RandomHelper.randString(), 1D));
		m.addUnit(new Unit(RandomHelper.randString(), 2.2));
		m.addUnit(new Unit(RandomHelper.randString(), 343.2));

		{ // add measurand to db
			try {
				persistence.addMeasurand(m, null);

				assertTrue(
						"measurand '" + m.getName() + "' should exist in db",
						persistence.getMeasurands().contains(m));
			} catch (AlreadyDefinedException e) {
				fail(e.getMessage());
			} catch (PersistenceException e) {
				fail(e.getMessage());
			}
		}
		
		{ // get units
			List<Unit> units = m.getUnits();
			assertTrue("measurand '"+m.getName()+"' should have exactely 3 units", units.size() == 3);
		}

	}
}
